﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Common.UIFunction
{
    public class DataGridViewBound
    {
        DataGridView DGV;
        int[] ColumnIndexMerge;
        int[] RowIndexMerge;
        public DataGridViewBound(DataGridView dgv)
        {
            this.DGV = dgv;
        }

        
        public void BoundColumn(string[] headertext, string[] columnname, 
            string[] datapropertyname)
        {
            int[] width = new int[headertext.Length];
            for (int i = 0; i < headertext.Length; i++)
                width[i] = 100;
            this.BoundColumn(headertext, columnname, datapropertyname, width);
        }
        public void BoundColumn(string[] headertext, string[] columnname,
            string[] datapropertyname, int[] width)
        {
            DGV.BackgroundColor = System.Drawing.Color.White;
            DGV.AutoGenerateColumns = false;
            DGV.AllowUserToAddRows = false;
            DGV.AllowUserToDeleteRows = false;
            DGV.RowHeadersVisible = false;
            DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            DGV.Columns.Clear();

            for (int i = 0; i < headertext.Length; i++)
            {
                DataGridViewTextBoxColumn newcolumn = new DataGridViewTextBoxColumn();
                newcolumn.Name = columnname[i];
                newcolumn.DataPropertyName = datapropertyname[i];
                newcolumn.HeaderText = headertext[i];
                newcolumn.Width = width[i];
                newcolumn.ReadOnly = true;
                DGV.Columns.Add(newcolumn);
            }
        }

        public void AddColumn(string headertext, string name, int width, bool isreadonly)
        {
            DataGridViewTextBoxColumn newcolumn = new DataGridViewTextBoxColumn();
            newcolumn.Name = name;
            newcolumn.HeaderText = headertext;
            newcolumn.Width = width;
            newcolumn.ReadOnly = isreadonly;
            this.DGV.Columns.Add(newcolumn);
        }

        public void InsertColumn(int columnindex, string headertext, string name, int width, bool isreadonly)
        {
            DataGridViewTextBoxColumn newcolumn = new DataGridViewTextBoxColumn();
            newcolumn.Name = name;
            newcolumn.HeaderText = headertext;
            newcolumn.Width = width;
            newcolumn.ReadOnly = isreadonly;
            this.DGV.Columns.Insert(columnindex, newcolumn);
        }

        

        public void AddColumn(string[] headertext, string[] name, int[] width, bool[] isreadonly)
        {
            for (int i = 0; i < headertext.Length; i++)
            {
                this.AddColumn(headertext[i], name[i], width[i], isreadonly[i]);
            }
        }

        public void AddCheckBoxColumn(string headertext, string name, int width)
        {
            DataGridViewCheckBoxColumn newcolumn = new DataGridViewCheckBoxColumn();
            newcolumn.HeaderText = headertext;
            newcolumn.Name = name;
            newcolumn.Width = width;
            this.DGV.Columns.Insert(0, newcolumn);
        }

        public void AddButtonColumn(string headertext, string text, string name, int width)
        {
            DataGridViewButtonColumn newcolumn = new DataGridViewButtonColumn();
            newcolumn.HeaderText = headertext;
            newcolumn.Text = text;
            newcolumn.Name = name;
            newcolumn.Width = width;
            newcolumn.UseColumnTextForButtonValue = true;
            this.DGV.Columns.Add(newcolumn);
        }
        public void InsertButtonColumn(int columnindex, string headertext, string text, string name, int width)
        {
            DataGridViewButtonColumn newcolumn = new DataGridViewButtonColumn();
            newcolumn.HeaderText = headertext;
            newcolumn.Text = text;
            newcolumn.Name = name;
            newcolumn.Width = width;
            newcolumn.UseColumnTextForButtonValue = true;
            this.DGV.Columns.Insert(columnindex, newcolumn);
        }

        public void MergeColumnCellSameContext(int[] columnindex)
        {
            this.ColumnIndexMerge = columnindex;
            this.DGV.CellPainting += new DataGridViewCellPaintingEventHandler(DGV_CellPaintingMergeColumnCell);
        }

        public void MergeColumnCellSameContextHavingKey(int[] columnindex)
        {
            this.ColumnIndexMerge = columnindex;
            this.DGV.CellPainting += new DataGridViewCellPaintingEventHandler(DGV_CellPaintingMergeColumnCellHavingKey);
        }

        
        private void DGV_CellPaintingMergeColumnCell(object sender, DataGridViewCellPaintingEventArgs e)
        {
            try
            {
                if (e.RowIndex == -1)
                    return;
                for (int i = 0; i < this.ColumnIndexMerge.Length; i++)
                    if (e.ColumnIndex == this.ColumnIndexMerge[i])
                    {
                        Brush gridBrush = new SolidBrush(this.DGV.GridColor),
                            backColorBrush = new SolidBrush(e.CellStyle.BackColor);
                        Pen gridLinePen = new Pen(gridBrush);
                        // 擦除原单元格背景
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // 画右边线
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                            e.CellBounds.Top, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom);

                        // 画 Grid 边线（仅画单元格的底边线和右边线）
                        //   如果下一行和当前行的数据不同，则在当前的单元格画一条底边线
                        if (e.RowIndex < this.DGV.Rows.Count - 1 &&
                        this.DGV.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=
                        e.Value.ToString())
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);//下边缘线
                        if (e.RowIndex == this.DGV.Rows.Count - 1)
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                            e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                            e.CellBounds.Bottom - 1);//下边缘线



                        //画（填写）单元格内容，相同的内容的单元格只填写第一个
                        if (e.Value != null)
                        {
                            if (e.RowIndex > 0 &&
                            this.DGV.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
                            e.Value.ToString())
                            { }
                            else
                            {
                                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                    Brushes.Black, e.CellBounds.X + 2,
                                    e.CellBounds.Y + 3, StringFormat.GenericDefault);
                            }
                        }
                        e.Handled = true;

                        break;
                    }
            }
            catch (Exception ex)
            {
            }
        }

        private void DGV_CellPaintingMergeColumnCellHavingKey(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            for (int i = 0; i < this.ColumnIndexMerge.Length; i++)
                if (e.ColumnIndex == this.ColumnIndexMerge[i])
                {
                    int keycolumnindex = this.ColumnIndexMerge[0];
                    Brush gridBrush = new SolidBrush(this.DGV.GridColor),
                        backColorBrush = new SolidBrush(e.CellStyle.BackColor);
                    Pen gridLinePen = new Pen(gridBrush);
                    // 擦除原单元格背景
                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                    // 画右边线
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                        e.CellBounds.Top, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom);

                    // 画 Grid 边线（仅画单元格的底边线和右边线）
                    //   如果下一行和当前行的数据不同，则在当前的单元格画一条底边线
                    if (e.RowIndex < this.DGV.Rows.Count - 1 &&
                    this.DGV.Rows[e.RowIndex + 1].Cells[keycolumnindex].Value.ToString() !=
                    this.DGV.Rows[e.RowIndex].Cells[keycolumnindex].Value.ToString())
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                        e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom - 1);//下边缘线
                    if (e.RowIndex == this.DGV.Rows.Count - 1)
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                        e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                        e.CellBounds.Bottom - 1);//下边缘线



                    //画（填写）单元格内容，相同的内容的单元格只填写第一个
                    if (e.Value != null)
                    {
                        if (e.RowIndex > 0 &&
                        this.DGV.Rows[e.RowIndex - 1].Cells[keycolumnindex].Value.ToString() ==
                    this.DGV.Rows[e.RowIndex].Cells[keycolumnindex].Value.ToString())
                        { }
                        else
                        {
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
                                Brushes.Black, e.CellBounds.X + 2,
                                e.CellBounds.Y + 3, StringFormat.GenericDefault);
                        }
                    }
                    e.Handled = true;

                    break;
                }
        }

        
    }
}
